<?php
!defined('P_W') && exit('Forbidden');
require_once (R_P . 'lib/base/systemdata.php');

class PW_PriceSource extends SystemData {
	
	var $_lang = array(
		'title'	=> '楼盘名称',
		'area'	=> '所属区域',
		'aveprice'	=> '价格',
		'opendate' => '开盘日期',
	);
	
	/**
	 * 根据配置信息获得文章排行数据
	 * @param Array $config
	 * @param int $num
	 */
	function getSourceData($config, $num) {
		$config = $this->_initConfig($config);
		$_tmp = $this->_getData($config['sorttype'],$config['price'],$num);
		foreach ($_tmp as $key => $value) {
			$_tmp[$key] = $this->_cookData($value);
		}
		return $_tmp;
	}
	
	/* (non-PHPdoc)
	 * @see lib/base/SystemData#getSourceConfig()
	 */
	function getSourceConfig() {
		return array(
			'price' => array(
				'name' => '价格区间',
				'type' => 'select',
				'value' => $this->_getPrice()
			),
			'sorttype' => array(
				'name' => '排序类型',
				'type' => 'select',
				'value' => array(
					'newHouse'  => '最新开盘',
					'hotHouse'  => '热门楼盘',
					'groupBuyHouse' 	=> '团购排行',
					'scoreHouse' 	=> '评价排行',
					'collectHouse' 	=> '收藏排行',
					'recommendHouse' => '户型推荐',
				)
			),
		);
	}
	

	
	/**
	 * 根据类型获得文章排行数据
	 * @param string $type
	 * @param int $num
	 */
	function _getData($type,$price,$num) {
		$houseSortElementService = $this->_getHouseSortElementService();
		switch ($type) {
			case 'groupBuyHouse':
				return $houseSortElementService->groupBuyHouse($price,$num,'price');
			case 'scoreHouse':
				return $houseSortElementService->scoreHouse($price,$num,'price');
			case 'collectHouse':
				return $houseSortElementService->collectHouse($price,$num,'price');
			case 'recommendHouse':
				return $houseSortElementService->recommendHouse($price,$num,'price');
			case 'newHouse':
				return $houseSortElementService->newHouse($price, $num,'price');
			case 'hotHouse':
				return $houseSortElementService->hotHouse($price, $num,'price');
			case 'modelHouse':
				return $houseSortElementService->modelHouse($price, $num,'price');
			default:
				return array();
		}
	}
	

	/**
	 * 格式化数据统一输出
	 * @param array $data
	 * @return array
	 */
	function _cookData($data) {
		global $db_bbsurl;
		//$data['url'] = $db_bbsurl . '/' . getArticleUrl($data['article_id']);
		$data['title'] = strip_tags($data['name']);
		return $data;
	}
	
	/**
	 * @param array $config
	 * @return array
	 */
	function _initConfig($config) {
		$temp = array();
		$temp['sorttype'] = isset($config['sorttype']) ? $config['sorttype'] : '';
		$temp['price'] = (isset($config['price']) && $config['price'] != 'all') ? $config['price'] : 0;
		return $temp;
	}
	
	function _getPrice(){
		$housevRangeDB = $this->_getHousevRangeDB();
		$vRange = $housevRangeDB->getByType('aveprice');
		$range = array('all' => '所有区间');
		foreach($vRange as $value){
			$range[$value['id']] = $value['fieldvalue'];
		}
		return $range;
	}
	
	function _getHouseSortElementService(){
		return House::loadClass('housesortelement');
	} 
	
	function _getHousevRangeDB(){
 		return House::loadDB('housevrange'); 
	}
	
	
}